package com.overlord.im.client.netty.channel;

import com.overlord.im.client.netty.handler.ClientHeartBeatHandler;
import com.overlord.im.client.netty.handler.ImClientHandler;
import com.overlord.im.commons.message.MessageDecoder;
import com.overlord.im.commons.message.MessageEncoder;
import com.overlord.im.commons.message.Protocol;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName ImServerChannelInitializer
 * @Author wuwei
 * @Description NONE
 * @Date 2020/5/9 11:20
 **/
public class ImClientChannelInitializer extends ChannelInitializer<Channel> {
    private Protocol protocol;
    private ImClientHandler imClientHandler;

    public ImClientChannelInitializer(Protocol protocol, ImClientHandler handler) {
        imClientHandler = handler;
        this.protocol = protocol;
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast("idleHandler", new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS));
        pipeline.addLast("decoder", new MessageDecoder(protocol));
        pipeline.addLast("encoder", new MessageEncoder(protocol));
        pipeline.addLast("heartBeatHandler", new ClientHeartBeatHandler());
        pipeline.addLast("clientHandler", imClientHandler);
    }
}
